<?php
//最近评论插件
class cy_comment extends WP_Widget {

	function cy_comment() {
		$widget_ops = array('description' => '显示粉丝们最新的评论');
		$this->WP_Widget('cy_comment', '&#xE000; 最新评论', $widget_ops);
	}

	function form($instance) {
		$title = isset($instance['title']) ? $instance['title'] : '最新评论';
		$limit = isset($instance['limit']) ? $instance['limit'] : 8;
		$img_size = isset($instance['img_size']) ? $instance['img_size'] : 40;
		$count = isset($instance['count']) ? $instance['count'] : 48;
?>
	<p>
		<label>标题：
			<input class="widefat" id="<?php echo $this->get_field_id('title')?>" name="<?php echo $this->get_field_name('title')?>" type="text" value="<?php echo $title?>" />
		</label>
	</p>

	<p>
		<label>评论数量：
			<input id="<?php echo $this->get_field_id('limit')?>" name="<?php echo $this->get_field_name('limit')?>" type="text" value="<?php echo $limit?>" size="6" />
		</label>
	</p>

	<p>
		<label>头像大小：
			<input id="<?php echo $this->get_field_id('img_size')?>" name="<?php echo $this->get_field_name('img_size')?>" type="text" value="<?php echo $img_size?>" size="6" />
		</label>
	</p>

	<p>
		<label>评论字数：
			<input id="<?php echo $this->get_field_id('count')?>" name="<?php echo $this->get_field_name('count')?>" type="text" value="<?php echo $count?>" size="6" />
		</label>
	</p>

	<p>
		<label>排除管理员评论：
			<input id="<?php echo $this->get_field_id('noadmin')?>" name="<?php echo $this->get_field_name('noadmin')?>" type="checkbox" <?php isset($instance['noadmin']) ? $checked = "checked=\"checked\"": $checked = ""; echo $checked?>/>
		</label>
	</p>

<?php
	}

	function update($new_instance, $old_instance) { return $new_instance; }

	function widget($args, $instance) {
		extract( $args );
		$title = $instance['title'];
		$limit = $instance['limit'];
		$img_size = $instance['img_size'];
		$count = $instance['count'];
		$noadmin = isset($instance['noadmin']) ? "AND user_id != 1" : "";
		echo $before_widget.$before_title.$title.$after_title;
		echo cy_widget_comment($img_size, $limit, $count, $noadmin);
		echo $after_widget;
	}
}
register_widget('cy_comment');

function cy_widget_comment($img_size, $limit, $count, $noadmin){
?> 
<ul class="cy_comment">
<?php 
	global $wpdb;
	$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, 
	SUBSTRING(comment_content,1,$count) AS com_excerpt
	FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' $noadmin ORDER BY comment_date_gmt DESC LIMIT $limit ";
	$comments = $wpdb->get_results($sql);
	foreach ($comments as $comment) {
		$avatar = get_avatar( $comment->comment_author_email, $img_size );
		$left = $img_size + 10;
		if($comment->comment_author == "") $comment->comment_author = "Anonymous"; # 匿名用户
		$output.= "	<li><div class=\"cmt-body\" style=margin-left:{$left}px>";
		if( get('ini_type')=='显示' ){
			$output.= "<img class=\"avatar\" src=\"".TPDIR."/res/avatar.jpg\" alt=\"avatar\" data-original=\"".preg_replace(array('/^.+(src=)(\"|\')/i', '/(\"|\')\sclass=(\"|\').+$/i'), array('', ''), $avatar )."\" width=$img_size height=$img_size style=left:-{$left}px";
			if(isIE8()) $output.= ";width:{$img_size}px;height:{$img_size}px";
			$output.= " />";
		}
		$content = do_shortcode( strip_tags($comment->com_excerpt) );
		if ( ereg('乃们别偷看', $content) ) $content = "悄悄话，乃们别偷看！";
		$output.= "<div class=\"cmt-msg\"><strong>{$comment->comment_author}：</strong>$content</div><p>发表在:<a href=\"".get_permalink($comment->ID)."#comment-{$comment->comment_ID}\"> {$comment->post_title}</a></p></div></li>\n";
	}
	$output = convert_smilies($output);
	echo $output;
?>
</ul>
<?php }?>